package com.example.equipmentmanage

import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.PopupMenu
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.equipmentmanage.adapter.SheBeiAdapter
import com.example.equipmentmanage.adapter.SheBeiSearchAdapter
import com.example.equipmentmanage.databinding.ActivitySheBeiBinding
import com.example.equipmentmanage.entity.SheBeiEntityItem

import com.example.equipmentmanage.utils.SheBeiViewModel
import com.example.lib_base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/equipmentmanage/SheBeiActivity")
@AndroidEntryPoint
class SheBeiActivity : BaseActivity<ActivitySheBeiBinding, SheBeiViewModel>() {

    lateinit var sheBeiAdapter: SheBeiAdapter
    lateinit var sheBeiSearchAdapter: SheBeiSearchAdapter


    override fun initView() {
        vm.SheBei()
        sheBeiAdapter= SheBeiAdapter()
        binding.rvShebei.adapter=sheBeiAdapter
        binding.rvShebei.layoutManager=LinearLayoutManager(this)
        sheBeiSearchAdapter= SheBeiSearchAdapter()
        binding.searchrv.adapter=sheBeiSearchAdapter
        binding.searchrv.layoutManager=LinearLayoutManager(this)

        binding.back.setOnClickListener {
            finish()
        }
      sheBeiAdapter.setOnItemClickListener { adapter, view, position ->
          var item=sheBeiAdapter.getItem(position) as SheBeiEntityItem
          ARouter.getInstance().build("/equipmentmanage/SheDetailActivity").withInt("id",item.id).navigation()
      }


       binding.shebeiAdd.setOnClickListener { view ->
            val popupMenu = PopupMenu(this, view)
            popupMenu.menu.add("新建设备信息")
            popupMenu.setOnMenuItemClickListener { item: MenuItem ->
                if (item.title == "新建设备信息") {
                   ARouter.getInstance().build("/equipmentmanage/SheBeiAddActivity").navigation()
                    true
                } else {
                    false
                }
            }
            popupMenu.show()
        }


        // 左滑删除
        val helper = ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0, left)
            }
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }
            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.layoutPosition
                vm.SheDel(sheBeiAdapter.data[position].id)
                sheBeiAdapter.data.removeAt(position)
                sheBeiAdapter.notifyDataSetChanged()
            }
        })
        helper.attachToRecyclerView(binding.rvShebei)


        // 搜索功能
        binding.searchShebei.setOnQueryTextListener(object : android.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (!query.isNullOrEmpty()) {
                    vm.SheQuery(query)
//                    // 隐藏软键盘
//                    val imm = getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as android.view.inputmethod.InputMethodManager
//                    imm.hideSoftInputFromWindow(binding.seach.windowToken, 0)
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()) {
                    // 搜索框清空，恢复正常列表
                    binding.rvShebei.visibility = View.VISIBLE
                    binding.searchrv.visibility = View.GONE
                    sheBeiSearchAdapter.data.clear()
                    sheBeiSearchAdapter.notifyDataSetChanged()
                }
                return false
            }
        })

    }

    override fun initData() {
        vm.SheBeiSuccess.observe(this){
            sheBeiAdapter.data.clear()
            sheBeiAdapter.data.addAll( it)
            sheBeiAdapter.notifyDataSetChanged()
        }
        vm.SheQuerySuccess.observe( this){
            sheBeiSearchAdapter.data.clear()
            sheBeiSearchAdapter.data.addAll(it)
            sheBeiSearchAdapter.notifyDataSetChanged()
            binding.rvShebei.visibility = View.GONE
            binding.searchrv.visibility = View.VISIBLE
        }
        vm.DelSheSuccess.observe( this){
            ToastUtils.showShort("删除成功")
        }
    }

    override fun onResume() {
        super.onResume()
        vm.SheBei()
    }

}